﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.COMMON.Core;
using Twi.Pages.SCS.Model;
using Twi.Pages.SCS.BLL;

namespace Twi.Pages.SCS
{
    public class OutRecord
    {
        /// <summary>
        /// 新增记录
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public TwiReturn NewRecord(string data)
        {
            dynamic dyData = JSSerialize.Deserialize<dynamic>(data);
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                TUser user = (TUser)UserContext.CurrentUser;
                TOutRecord outRecord = new TOutRecord();
                outRecord.FID = Guid.NewGuid();
                outRecord.FTypeID = new Guid(dyData["FTypeID"]);
                outRecord.FTypeName = dyData["FTypeName"];
                outRecord.FTitle = dyData["FTitle"];
                outRecord.FUserRealNames = dyData["FUserRealNames"];
                outRecord.FStartDate = dyData["FStartDate"];
                outRecord.FEndDate = dyData["FEndDate"];
                outRecord.FTimeSection = Convert.ToInt32(dyData["FTimeSection"]);
                outRecord.FTimeSection2 = Convert.ToInt32(dyData["FTimeSection2"]);
                outRecord.FAuditor = dyData["FAuditor"];
                outRecord.FAuditorID = new Guid(dyData["FAuditorID"]);
                outRecord.FAuditDate = dyData["FAuditDate"];
                outRecord.FDetail = dyData["FDetail"];
                outRecord.FOperator = user.FRealName;
                outRecord.FOperatorID = user.FID;
                outRecord.FOperateDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                if (dyData["FTypeID"] == "1f1d20cb-31d0-4537-b264-9144102c155f") //临时外出
                {
                    outRecord.FBeginTime = dyData["FBeginTime"];
                    outRecord.FEndTime = dyData["FEndTime"];
                }
                else
                {
                    //开始结束日期相同，但所选包含上午和下午，那么意思就是全天
                    if (outRecord.FStartDate == outRecord.FEndDate && outRecord.FTimeSection !=outRecord.FTimeSection2)
                    {                        
                        outRecord.FTimeSection = 1;
                        outRecord.FTimeSection2 = 1;
                    }
                }
                List<TOutRecordDetail> outRecordDetails = new List<TOutRecordDetail>();
                string strUserIDs = "" + dyData["UserIDs"];
                string[] userIDs = strUserIDs.Split(',');
                string[] userRealNames = outRecord.FUserRealNames.Split(',');                
                if (outRecord.FStartDate == outRecord.FEndDate) //同一天
                {
                    for (int i = 0; i < userIDs.Length; i++)
                    {
                        string userID = userIDs[i];
                        string userRealName = userRealNames[i];
                        TOutRecordDetail model = new TOutRecordDetail();
                        model.FID = Guid.NewGuid();
                        model.FOutRecordID = outRecord.FID;
                        model.FTypeID = outRecord.FTypeID;
                        model.FTypeName = outRecord.FTypeName;
                        model.FUserID = new Guid(userID);
                        model.FUserRealName = userRealName;
                        model.FDate = outRecord.FStartDate;
                        model.FTimeSection = outRecord.FTimeSection;
                        model.FBeginTime = outRecord.FBeginTime;
                        model.FEndTime = outRecord.FEndTime;
                        outRecordDetails.Add(model);
                    }
                }
                else //非同一天
                {
                    DateTime startDate = Convert.ToDateTime(outRecord.FStartDate);
                    DateTime endDate = Convert.ToDateTime(outRecord.FEndDate);
                    int days = endDate.Subtract(startDate).Days;
                    for (int j = 0; j < days;j++)
                    {
                        for (int i = 0; i < userIDs.Length; i++)
                        {
                            string userID = userIDs[i];
                            string userRealName = userRealNames[i];
                            TOutRecordDetail model = new TOutRecordDetail();
                            model.FID = Guid.NewGuid();
                            model.FOutRecordID = outRecord.FID;
                            model.FTypeID = outRecord.FTypeID;
                            model.FTypeName = outRecord.FTypeName;
                            model.FUserID = new Guid(userID);
                            model.FUserRealName = userRealName;
                            model.FDate = startDate.AddDays(j).ToString("yyyy-MM-dd");
                            model.FTimeSection = outRecord.FTimeSection;
                            model.FBeginTime = outRecord.FBeginTime;
                            model.FEndTime = outRecord.FEndTime;
                            if (j==0)
                            {
                                model.FTimeSection = outRecord.FTimeSection;
                            }
                            else if (j == days)
                            {
                                model.FTimeSection = outRecord.FTimeSection2;
                            }
                            else
                            {
                                model.FTimeSection = 1;
                            }
                            outRecordDetails.Add(model);
                        }
                    }                    
                }
                _OutRecord.Instance.NewRecord(outRecord, outRecordDetails);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        public TwiReturn GetByCondition(string startIndex="0", string pageSize="25", string beginDate=null,string endDate=null,string keyValues=null)
        {
            TwiReturn twi = new TwiReturn();            
            twi.rightName = "pass";
            try {
                UserContext.CheckRight(ref twi);
                PagingCondition paging = new PagingCondition();
                paging.startIndex = Convert.ToInt32(startIndex);
                paging.pageSize =Convert.ToInt32(pageSize);
                MultiCondition multiCondition = new MultiCondition();
                if (!string.IsNullOrEmpty(beginDate))
                {
                    multiCondition.BeginDate = Convert.ToDateTime(beginDate);
                }
                if (!string.IsNullOrEmpty(endDate))
                {
                    multiCondition.EndDate = Convert.ToDateTime(endDate);
                }
                if (!string.IsNullOrEmpty(keyValues))
                {
                    multiCondition.Dict = TwiJsonHelper.JsonToDictionary(keyValues);
                }
                int totalCount = 0;
                var list = _OutRecord.Instance.GetByCondition(paging, multiCondition, ref totalCount);
                twi.totalCount = totalCount;
                twi.data = JSSerialize.Serialize(list);
                twi.success = true;
            }
            catch(System.Exception ex){
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="outRecordId"></param>
        /// <returns></returns>
        public TwiReturn Delete(string outRecordId)
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                _OutRecord.Instance.Delete(outRecordId);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;	
            }
            return twi;
        }
        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TwiReturn GetEntityByID(string id)
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                var entity = _OutRecord.Instance.GetEntityByID(id);
                twi.data = JSSerialize.Serialize(entity);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
        /// <summary>
        /// 编辑记录单详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newDetail"></param>
        /// <returns></returns>
        public TwiReturn EditDetail(string id, string newDetail)
        {
            TwiReturn twi = new TwiReturn();
            twi.rightName = "pass";
            try
            {
                UserContext.CheckRight(ref twi);
                _OutRecord.Instance.EditDetail(id, newDetail);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
    }
}
